package com.wq.sofa.framework.bolt;

import com.alipay.remoting.Connection;
import com.alipay.remoting.ConnectionEventProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: wangqiang20995
 * @Date:2018/9/19
 * @Description:
 * @Resource:
 */
public class DisConnectEventProcessor implements ConnectionEventProcessor {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private AtomicBoolean dicConnected    = new AtomicBoolean();
    private AtomicInteger disConnectTimes = new AtomicInteger();

    @Override
    public void onEvent(String remoteAddr, Connection conn) {
        if(conn == null){
            throw new RuntimeException("建立连接对象Connection为空");
        }
        dicConnected.set(true);
        disConnectTimes.incrementAndGet();
        logger.info("release a connection --> ",remoteAddr);
    }

    public boolean isDisConnected() {
        return this.dicConnected.get();
    }

    public int getDisConnectTimes() {
        return this.disConnectTimes.get();
    }

    public void reset() {
        this.disConnectTimes.set(0);
        this.dicConnected.set(false);
    }
}
